package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.view.View;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.db.LLSourceHomepageDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.SourceHomepageBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.uiutil.LLSceneLinkageUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class RoomEnvironmentDataSourceAcitvity extends DataSourceDeviceActivity{

    @Override
    public void onRightClick(View view) {
        DeviceBean device = mDataSourceDeviceAdapter.getSelected();
        String roomSerialNo = getString("room_serial_no");
        int environmentType = getInt("environment_type");
        String areaSerialNo = getString("area_serial_no");
        Intent data = new Intent();
        if(areaSerialNo == null){
            Map<String, Object> map = new HashMap<>();
            map.put("roomSerialNo",roomSerialNo);
            map.put("areaType",1);
            map.put("deviceSerialNo",device.getDeviceSerialNo());
            map.put("areaShowType",environmentType);
            map.put("isHomePage",true);
            map.put("stateName", LLSceneLinkageUtil.getLinkageState(environmentType));
//            map.put("stateValue",);
            map.put("houseSerialNo", AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo());
            LLHttpManager.addRoomEnvironemnt(this, map, new HttpCallback<HttpData<SourceHomepageBean>>(this){
                @Override
                public void onStart(Call call) {
                    showDialog();
                }

                @Override
                public void onSucceed(HttpData<SourceHomepageBean> result) {
                    super.onSucceed(result);
                    SourceHomepageBean bean = result.getData();
                    LLSourceHomepageDBManager.getInstance(getContext()).put(bean);
                    setResult(RESULT_OK, data);
                    finish();
                    toast(R.string.set_success);
                }

                @Override
                public void onFail(Exception e) {
                    super.onFail(e);
                }

                @Override
                public void onEnd(Call call) {
                    hideDialog();
                }
            });

        }else{
            LLHttpManager.setRoomEnvironemntFromDevice(this, areaSerialNo, device.getDeviceSerialNo(), new HttpCallback<HttpData<Void>>(this){
                @Override
                public void onStart(Call call) {
                    showDialog();
                }

                @Override
                public void onSucceed(HttpData<Void> result) {
                    super.onSucceed(result);
                    LLSourceHomepageDBManager.getInstance(getContext()).changeFromDevice(areaSerialNo,device.getDeviceSerialNo());
                    setResult(RESULT_OK, data);
                    finish();
                    toast(R.string.set_success);
                }

                @Override
                public void onFail(Exception e) {
                    super.onFail(e);
                }

                @Override
                public void onEnd(Call call) {
                    hideDialog();
                }
            });
        }



        data.putExtra("floor_room_name", LLRoomDBManager.getInstance(this).getFloorRoomName(device.getRoomSerialNo()));
        data.putExtra("room_serial_no", device.getRoomSerialNo());
        data.putExtra("device_name", device.getName());
        data.putExtra("device_serial_no", device.getDeviceSerialNo());
        setResult(RESULT_OK, data);
    }
}
